﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace SaveTheQueen.Utils
{
    public static class GamepadInput
    {
        private static GamePadState gpsLast = GamePad.GetState(PlayerIndex.One);
        private static GamePadState gpsActual = GamePad.GetState(PlayerIndex.One);

        private static bool lockInput;

        public static void Pool()
        {
            gpsLast = gpsActual; // swap states
            gpsActual = GamePad.GetState(PlayerIndex.One); //refresh state
        }

        public static void LockInput(bool lockState)
        { lockInput = lockState; }
        private static Button CheckButton(ButtonState actual, ButtonState last)
        {
            if (!lockInput)
            {
                if (actual == ButtonState.Pressed && actual != last)
                    return Button.Down; // if actual is pressed, but last is not.
                else if (actual == ButtonState.Pressed && actual == last)
                    return Button.Held; // if actual and last is pressed
                else // if actual is released
                    return Button.Up;
            }
            else // if input is locked
                return Button.Up;
        }

        public static Button A
        {
            get
            {
                return CheckButton(gpsActual.Buttons.A, gpsLast.Buttons.A);
            }
        }
        public static Button B
        {
            get
            {
                return CheckButton(gpsActual.Buttons.B, gpsLast.Buttons.B);
            }
        }
        public static Button Y
        {
            get
            {
                return CheckButton(gpsActual.Buttons.Y, gpsLast.Buttons.Y);
            }
        }
        public static Button X
        {
            get
            {
                return CheckButton(gpsActual.Buttons.X, gpsLast.Buttons.X);
            }
        }
        public static Button L
        {
            get
            {
                return CheckButton(gpsActual.Buttons.LeftShoulder, gpsLast.Buttons.LeftShoulder);
            }
        }
        public static Button R
        {
            get
            {
                return CheckButton(gpsActual.Buttons.RightShoulder, gpsLast.Buttons.RightShoulder);
            }
        }
        public static Button Start
        {
            get
            {
                return CheckButton(gpsActual.Buttons.Start, gpsLast.Buttons.Start);
            }
        }
        public static Button BackButton
        {
            get
            {
                return CheckButton(gpsActual.Buttons.Back, gpsLast.Buttons.Back);
            }
        }
        public static Button LeftStickButton
        {
            get
            {
                return CheckButton(gpsActual.Buttons.LeftStick, gpsLast.Buttons.LeftStick);
            }
        }
        public static Button RightStickButton
        {
            get
            {
                return CheckButton(gpsActual.Buttons.RightStick, gpsLast.Buttons.RightStick);
            }
        }
        public static Button BigButton
        {
            get
            {
                return CheckButton(gpsActual.Buttons.BigButton, gpsLast.Buttons.BigButton);
            }
        }

        public static Button Up
        {
            get
            {
                return CheckButton(gpsActual.DPad.Up, gpsLast.DPad.Up);
            }
        }
        public static Button Down
        {
            get
            {
                return CheckButton(gpsActual.DPad.Down, gpsLast.DPad.Down);
            }
        }
        public static Button Left
        {
            get
            {
                return CheckButton(gpsActual.DPad.Left, gpsLast.DPad.Left);
            }
        }
        public static Button Right
        {
            get
            {
                return CheckButton(gpsActual.DPad.Right, gpsLast.DPad.Right);
            }
        }

        public static Button LeftTrigger
        {
            get
            {
                if (!lockInput)
                {
                    if (gpsActual.Triggers.Left > 0 && gpsLast.Triggers.Left > 0)
                        return Button.Held;
                    else if (gpsActual.Triggers.Left > 0 && gpsLast.Triggers.Left <= 0)
                        return Button.Down;
                    else
                        return Button.Up;
                }
                else
                    return Button.Up;
            }
        }
        public static Button RightTrigger
        {
            get
            {
                if (!lockInput)
                {
                    if (gpsActual.Triggers.Right > 0 && gpsLast.Triggers.Right > 0)
                        return Button.Held;
                    else if (gpsActual.Triggers.Right > 0 && gpsLast.Triggers.Right <= 0)
                        return Button.Down;
                    else
                        return Button.Up;
                }
                else
                    return Button.Up;
            }
        }

        public static float LeftTriggerRawValue
        {
            get { return gpsActual.Triggers.Left; }
        }
        public static float RightTriggerRawValue
        {
            get { return gpsActual.Triggers.Right; }
        }

        public static Vector2 LeftThumbstick
        {
            get { return gpsActual.ThumbSticks.Left; }
        }
        public static Vector2 RightThumbstick
        {
            get { return gpsActual.ThumbSticks.Right; }
        }
    }
}
